package com.kwan.xframe.record.audio;

import android.os.SystemClock;
import android.widget.Chronometer;

//计时器工具类，使用Chronometer计时
public class TimerManager {

	private Chronometer timer;
	//暂停时间
	private static long elapsedTime = 0;


	/**
	 * <Chronometer
	 *                     app:layout_constraintTop_toTopOf="parent"
	 *                     android:id="@+id/timer"
	 *                     android:layout_width="match_parent"
	 *                     android:layout_height="wrap_content"
	 *                     android:format="00:00:00"
	 *                     android:gravity="center"
	 *                     android:textSize="@dimen/txt_16" />
	 * @param t
	 */


	public void set(Chronometer t) {
		this.timer = t;
	}

	public void start() {
		//计时器清零
		timer.setBase(SystemClock.elapsedRealtime());
		int hour = (int) ((SystemClock.elapsedRealtime() - timer.getBase()) / 1000 / 3600);
		timer.setFormat("0" + hour + ":%s");
		timer.start();
	}

	public void pause() {
		timer.stop();
		elapsedTime = SystemClock.elapsedRealtime() - timer.getBase();
	}

	public void restart() {
		timer.setBase(SystemClock.elapsedRealtime() - elapsedTime);
		int hour = (int) ((SystemClock.elapsedRealtime() - timer.getBase()) / 1000 / 3600);
		timer.setFormat("0" + hour + ":%s");
		timer.start();
	}

	public void stop() {
		elapsedTime = 0;
		//计时器清零
		timer.setBase(SystemClock.elapsedRealtime());
		timer.setFormat("00:%s");
		timer.stop();
	}
}
